<template>
  <div id="samstoStorageFridgeList">
    <div class="bodyContent">
      <Splitpanes class="area">
        <Pane size="40">
          <el-card class="box-card">
            <div class='panel-body'>
              <vxe-toolbar perfect>
                <template #buttons>
                  <SelectMultDataGrid
                    :url="'/api/samsto/samstoFridge/getAllListNotLink'"
                    ref="selectMultDataGridRef"
                    :column="[
                  { field: 'boxCode', title: '编号' }, //列标题
                  { field: 'modelName', title: '冰箱型号' }
                ]"
                    :searchParam="{storageId:pCurSelectRow.id}"
                    :isPage="false"
                    @SelectCellClick="
                  (e) => {
                    saveMethod(e);
                  }

                ">
                  </SelectMultDataGrid>
                  <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="deleteMethod"></vxe-button>
                </template>
              </vxe-toolbar>
              <div class="tableBox">
                <vxe-table ref="tableRef" border="inner" align="left"
                           class="mytable-scrollbar" :loading="showLoading"
                           :data="tableData"
                           :row-config="{ isCurrent: true, isHover: true }"
                           :height="tableHeight"
                           :column-config="{ resizable: true }"
                           @cell-click="tableCellClick"
                           resizable
                           show-overflow>
                  <vxe-column field="fridgeId" title="冰箱编号" width="120" :edit-render="{ autofocus: '.vxe-input--inner' }">
                    <template #default="{ row }">
                      {{row.itemCode}}
                    </template>
                  </vxe-column>>
                  <vxe-column field="fridgeId" title="冰箱类型" width="180" :edit-render="{ autofocus: '.vxe-input--inner' }">
                    <template #default="{ row }">
                      {{row.itemName}}
                    </template>
                  </vxe-column>
                </vxe-table>
              </div>
            </div>
          </el-card>
        </Pane>

<!--        fixme: 后续增加关联层架功能-->
<!--        <Pane size="60">-->
<!--          <div class="bankContent">-->

<!--            <el-tabs type="border-card" v-model="activeName">-->
<!--              <el-tab-pane label="层架列表" name="infoForm" :disabled='infoTabPaneDisabled' >-->
<!--                <FridgeLayerList-->
<!--                  ref="fridgeLayerListRef"-->
<!--                  :currentRow="curSelectRow"-->
<!--                  :asknDicts="asknDicts"-->
<!--                  @findList="findList" />-->
<!--              </el-tab-pane>-->
<!--            </el-tabs>-->
<!--          </div>-->
<!--        </Pane>-->
      </Splitpanes>
    </div>
  </div>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import { reactive, ref, getCurrentInstance, onMounted, watch, PropType } from 'vue';
import { Splitpanes, Pane } from 'splitpanes';
import { VxeColumnPropTypes, VXETable } from 'vxe-table';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import type { VxeTableInstance} from 'vxe-table';
import { AppStore } from '@/stores/store';
import {  IsEmpty } from '@/utils/common';
import SelectMultDataGrid from '@/components/SelectMultDataGrid.vue';
import FridgeLayerList from '../list/FridgeLayerList.vue'
import {
  samstoStorageFridgeAllList,
  samstoStorageFridgeDelete, samstoStorageFridgeSaveAll, samstoStorageSaveAll
} from '@/views/lab_sys/samsto_set/apis/samsto_api';
let store = AppStore();

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();

// 左侧表格:
let showLoading = ref(false); //刷新+表格加载
const form = reactive({ keyvalue: '' }); //根据关键字查询
const tableRef = ref<VxeTableInstance<RowVO>>(); //表格ref标记
const tableData = reactive([]); //表格数据
const curSelectRow = ref({});
const pCurSelectRow = ref({});
const linkType = ref("fridge");
const asknDicts = reactive([]);
const isPageDisabled = ref(true);
let tableHeight = ref(window.innerHeight - 210);
// 右侧表单:
const selectMultDataGridRef = ref<InstanceType<typeof SelectMultDataGrid>>(); //基本信息表单标记ref
const fridgeLayerListRef = ref<InstanceType<typeof FridgeLayerList>>(); //基本信息表单标记ref
const activeName = ref('infoForm');
const infoTabPaneDisabled = ref(true);

watch(curSelectRow, (newValue, oldValue) => {
  if (!IsEmpty(newValue.id)){
    isPageDisabled.value=false;
    infoTabPaneDisabled.value=false;
  }else{
    activeName.value="infoForm";
    isPageDisabled.value=true;
    infoTabPaneDisabled.value=true;
  }
});

defineExpose({
  initPage
});
// 父组件传值
let props = defineProps({
//  infoDicts: { type: Array as PropType<any>, required: true },
  pCurSelectRow: { type: Object as PropType<any>, required: true }
}) as any;
watch(
  () => props.pCurSelectRow,
  (nval, oval) => {
    pCurSelectRow.value=nval;
  },
  {
    deep: true,
    immediate: true
  }
);
// 方法定义区 =============
onMounted(() => {
  findList(); // 获取题库数据
});
// 获取表格数据
async function findList() {
  showLoading.value = true;
  curSelectRow.value={};
  tableData.length = 0; //放入数据前清空数据
  const res: any = await samstoStorageFridgeAllList({
    keywords: form.keyvalue,
    storageId: pCurSelectRow.value.id
  })
  if (res.status.code == 1) {
    tableData.push(...res.data);
  }
  showLoading.value = false;
}

async function initPage(selsectRow:any) {
  pCurSelectRow.value=selsectRow;
  findList();
}
// 保存调用子菜单
const saveMethod = async (row) => {
  console.log(row,"saveMethod");
  let sfItems=[];
  row.forEach((item) => {
    let frItem={
      storageId:pCurSelectRow.value.id,
      fridgeId:item.id,
      linkType:linkType.value,
    };
    sfItems.push(frItem);
  });
    //批量一起新增
    const obj = {
      infoList: sfItems
    };
    let res: any = await samstoStorageFridgeSaveAll(obj);
    res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
    showLoading.value=false;
    findList();
}

// 删除
const deleteMethod = async () => {
  const deleteInfo = {
    cancelButtonClass: 'cancel-button',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  };
  if (IsEmpty(curSelectRow.value.id)) {
    VxeMessage('请先选中要删除的项', VxeNotifyType.warning);
    return;
  }
  await proxy.$confirm('确定删除此条数据？', '提示', deleteInfo);
  let ids = [curSelectRow.value.id];
  const res: any = await samstoStorageFridgeDelete(ids);
  res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
  await findList();
}

// 表格选中当前行
function tableCellClick({ row }: any) {
  curSelectRow.value = row;
  proxy.$forceUpdate();
  selectItemRows(curSelectRow.value);
}
//选中行后传值给FORM
async function selectItemRows(row) {
  let selectItem=row;
  fridgeLayerListRef.value?.initPage(selectItem);
}
</script>
<style lang="scss" scoped>
#samstoStorageFridgeList {
  width: 100%;
  height: 100%;
  .bodyContent {
    // 面包屑40px  上下间距10px
    height: calc(100% - 60px);
    .area {
      background-color: $base-white;
      // 附加按钮间距
      .button-blue,
      .button-red {
        margin: 5px 0 5px 5px;
      }
      .tableBox {
        height: calc(100% - 40px);
      }
      .el-tabs {
        border: none;
        height: 100%;
        :deep(.el-tabs__content) {
          padding:0;
          height: calc(100% - 39px);
          .el-tab-pane {
            height: 100%;
          }
          #pane-0 {
            padding:15px;
            height:calc(100% - 30px);
          }
        }
      }
    }
    .bankItemContent{
      min-height: 600px;
      border-radius: 10px;
    }

    .disabled {
      pointer-events: none;
      opacity: 0.5;
    }
  }
}
</style>

